package xcmg.device.web.agent;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.CompanyConstants;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.ServiceStaffQuotaService;
import xcmg.device.service.vo.agent.ServiceStaffQuotaQueryVO;
import xcmg.device.service.vo.agent.ServiceStaffQuotaVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

/**
 * ServiceStaffQuota Controller 实现类
 *
 * @author huhuawei
 */
@RestController
@RequestMapping("/serviceStaffQuota")
public class ServiceStaffQuotaController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(ServiceStaffQuotaController.class);
    @Autowired
    private ServiceStaffQuotaService serviceStaffQuotaService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "服务人员额度表分页列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<ServiceStaffQuotaVO>> list(@RequestBody SearchCommonVO<ServiceStaffQuotaQueryVO> condition) throws Exception {
        if (null == condition.getFilters()) {
            condition.setFilters(new ServiceStaffQuotaQueryVO());
        }
        condition.getFilters().setOrgId(getOrgId());
        condition.getFilters().setRoleName(CompanyConstants.INIT_ROLE_NAME);
        PageCommonVO<ServiceStaffQuotaVO> pageCommonVO = serviceStaffQuotaService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "服务人员额度表-保存")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> save(@RequestBody ServiceStaffQuotaVO serviceStaffQuotaVO) {
        if (null == serviceStaffQuotaVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            String userId=getUserId();
            String userName=getUserName();
            serviceStaffQuotaVO.setCreateId(userId);
            serviceStaffQuotaVO.setCreateName(userName);
            serviceStaffQuotaVO.setUpdateId(userId);
            serviceStaffQuotaVO.setStatus(1);
            ErrorCode code = serviceStaffQuotaService.save(serviceStaffQuotaVO);
            return actionResult(code);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }


    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "根据userId查询服务人员额度表详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceStaffQuotaVO> item(@RequestParam String userId) throws Exception {
        ServiceStaffQuotaQueryVO queryVO = new ServiceStaffQuotaQueryVO();
        queryVO.setUserId(userId);
        queryVO.setOrgId(getOrgId());
        queryVO.setRoleName(CompanyConstants.INIT_ROLE_NAME);
        return actionValueResult(serviceStaffQuotaService.queryItem(queryVO));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除服务人员额度表数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> delete(@RequestBody ServiceStaffQuotaVO serviceStaffQuotaVO) {
        ErrorCode errorCode = null;
        try {
            errorCode = serviceStaffQuotaService.batchDelete(serviceStaffQuotaVO.getListId());
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/batchDisable", method = RequestMethod.POST)
    @ApiOperation(value = "批量禁用")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> batchDisable(@RequestBody ServiceStaffQuotaVO serviceStaffQuotaVO) {
        ErrorCode errorCode = null;
        try {
            errorCode = serviceStaffQuotaService.batchDisable(serviceStaffQuotaVO.getListId());
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }

    @RequestMapping(value = "/batchEnable", method = RequestMethod.POST)
    @ApiOperation(value = "批量启用")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> batchEnable(@RequestBody ServiceStaffQuotaVO serviceStaffQuotaVO) {
        ErrorCode errorCode = null;
        try {
            errorCode = serviceStaffQuotaService.batchEnable(serviceStaffQuotaVO.getListId());
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }
}
